/**   
* @Title: UsersForeachHandler.java
* @Package gov.pbc.gx.hc.sa.uflo
* @Description: TODO(用一句话描述该文件做什么)
* @author WLF
* @date 2015-11-9 下午4:28:49
* @version V1.0
*/ 
package gov.pbc.nn.kjgl.task.handler;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bstek.bdf2.core.business.IUser;
import com.bstek.uflo.client.service.ProcessClient;
import com.bstek.uflo.env.Context;
import com.bstek.uflo.model.ProcessInstance;
import com.bstek.uflo.process.handler.ForeachHandler;

import gov.pbc.nn.core.bdf.entity.User;
import gov.pbc.nn.core.utils.SecurityUtils;
import gov.pbc.nn.kjgl.Constants;
import gov.pbc.nn.kjgl.task.dao.TaskDao;
import gov.pbc.nn.kjgl.task.entity.TaskBranchUser;
import gov.pbc.nn.kjgl.task.entity.TaskEntity;

/** 
 * @ClassName: UsersForeachHandler
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author WLF
 * @date 2015-11-9 下午4:28:49
 * 
 */
@Component("branchUserForeachHandler")
public class BranchUserForeachHandler implements ForeachHandler {

	/** (非 Javadoc) 
	 * Title: handle
	 * Description:
	 * @param arg0
	 * @param arg1
	 * @return
	 * @see com.bstek.uflo.process.handler.ForeachHandler#handle(com.bstek.uflo.model.ProcessInstance, com.bstek.uflo.env.Context)
	 */
	@Resource(name=ProcessClient.BEAN_ID)
	private ProcessClient processClient;
	
	@Autowired
	private TaskDao taskDao;
	
	@SuppressWarnings("unchecked")
	@Override
	public Collection<Object> handle(ProcessInstance processInstance, Context context) {
		// TODO Auto-generated method stub
		//return (Collection<Object>) processClient.getProcessVariable("branchUserList",processInstance);
		try {
			 Collection<TaskBranchUser> branchUsers=this.taskDao.getBranchUsersbyTaskId(processInstance.getBusinessId());
			 Collection<Object> params=new ArrayList<Object>();
			 for (TaskBranchUser branchUser : branchUsers) {
				Map<String, Object> param=new HashMap<String, Object>();
				param.put("branchId", branchUser.getId());
				if(branchUser.getBranchType().equals("南宁部门"))
					param.put("username", branchUser.getUsername());
				else if(branchUser.getBranchType().equals("地市科室")){
					List<IUser> users=SecurityUtils.getUserByRoleForDept(branchUser.getDeptId(),Constants.ROLE_TASK_FENBAN);
					List<String> usernames=new ArrayList<String>();
					for (IUser user : users) {
						usernames.add(user.getUsername());
					}
					param.put("username", usernames);
				}
				params.add(param);
			}
			 return params;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

}
